package com.netbetter.langzheng.commons.third.utils;

public class ApacheStringUtils {

  // ----------------------------------------------------------------------
  /**
   * <p>
   * Checks if an array of Objects is empty or <code>null</code>.
   * </p>
   * 
   * @param array the array to test
   * @return <code>true</code> if the array is empty or <code>null</code>
   * @since 2.1
   */
  public static boolean isEmpty(Object[] array) {
    if (array == null || array.length == 0) {
      return true;
    }
    return false;
  }

  /**
   * <p>
   * Checks if an array of primitive longs is empty or <code>null</code>.
   * </p>
   * 
   * @param array the array to test
   * @return <code>true</code> if the array is empty or <code>null</code>
   * @since 2.1
   */
  public static boolean isEmpty(long[] array) {
    if (array == null || array.length == 0) {
      return true;
    }
    return false;
  }

  /**
   * <p>
   * Checks if an array of primitive ints is empty or <code>null</code>.
   * </p>
   * 
   * @param array the array to test
   * @return <code>true</code> if the array is empty or <code>null</code>
   * @since 2.1
   */
  public static boolean isEmpty(int[] array) {
    if (array == null || array.length == 0) {
      return true;
    }
    return false;
  }

  /**
   * <p>
   * Checks if an array of primitive shorts is empty or <code>null</code>.
   * </p>
   * 
   * @param array the array to test
   * @return <code>true</code> if the array is empty or <code>null</code>
   * @since 2.1
   */
  public static boolean isEmpty(short[] array) {
    if (array == null || array.length == 0) {
      return true;
    }
    return false;
  }

  /**
   * <p>
   * Checks if an array of primitive chars is empty or <code>null</code>.
   * </p>
   * 
   * @param array the array to test
   * @return <code>true</code> if the array is empty or <code>null</code>
   * @since 2.1
   */
  public static boolean isEmpty(char[] array) {
    if (array == null || array.length == 0) {
      return true;
    }
    return false;
  }

  /**
   * <p>
   * Checks if an array of primitive bytes is empty or <code>null</code>.
   * </p>
   * 
   * @param array the array to test
   * @return <code>true</code> if the array is empty or <code>null</code>
   * @since 2.1
   */
  public static boolean isEmpty(byte[] array) {
    if (array == null || array.length == 0) {
      return true;
    }
    return false;
  }

  /**
   * <p>
   * Checks if an array of primitive doubles is empty or <code>null</code>.
   * </p>
   * 
   * @param array the array to test
   * @return <code>true</code> if the array is empty or <code>null</code>
   * @since 2.1
   */
  public static boolean isEmpty(double[] array) {
    if (array == null || array.length == 0) {
      return true;
    }
    return false;
  }

  /**
   * <p>
   * Checks if an array of primitive floats is empty or <code>null</code>.
   * </p>
   * 
   * @param array the array to test
   * @return <code>true</code> if the array is empty or <code>null</code>
   * @since 2.1
   */
  public static boolean isEmpty(float[] array) {
    if (array == null || array.length == 0) {
      return true;
    }
    return false;
  }

  /**
   * <p>
   * Checks if an array of primitive booleans is empty or <code>null</code>.
   * </p>
   * 
   * @param array the array to test
   * @return <code>true</code> if the array is empty or <code>null</code>
   * @since 2.1
   */
  public static boolean isEmpty(boolean[] array) {
    if (array == null || array.length == 0) {
      return true;
    }
    return false;
  }

  // Replacing
  // -----------------------------------------------------------------------
  /**
   * <p>
   * Replaces a String with another String inside a larger String, once.
   * </p>
   * <p>
   * A <code>null</code> reference passed to this method is a no-op.
   * </p>
   * 
   * <pre>
   * StringUtils.replaceOnce(null, *, *)        = null
   * StringUtils.replaceOnce(&quot;&quot;, *, *)          = &quot;&quot;
   * StringUtils.replaceOnce(&quot;any&quot;, null, *)    = &quot;any&quot;
   * StringUtils.replaceOnce(&quot;any&quot;, *, null)    = &quot;any&quot;
   * StringUtils.replaceOnce(&quot;any&quot;, &quot;&quot;, *)      = &quot;any&quot;
   * StringUtils.replaceOnce(&quot;aba&quot;, &quot;a&quot;, null)  = &quot;aba&quot;
   * StringUtils.replaceOnce(&quot;aba&quot;, &quot;a&quot;, &quot;&quot;)    = &quot;ba&quot;
   * StringUtils.replaceOnce(&quot;aba&quot;, &quot;a&quot;, &quot;z&quot;)   = &quot;zba&quot;
   * </pre>
   * 
   * @see #replace(String text, String repl, String with, int max)
   * @param text text to search and replace in, may be null
   * @param repl the String to search for, may be null
   * @param with the String to replace with, may be null
   * @return the text with any replacements processed, <code>null</code> if
   *         null String input
   */
  public static String replaceOnce(String text, String repl, String with) {
    return replace(text, repl, with, 1);
  }

  /**
   * <p>
   * Replaces all occurrences of a String within another String.
   * </p>
   * <p>
   * A <code>null</code> reference passed to this method is a no-op.
   * </p>
   * 
   * <pre>
   * StringUtils.replace(null, *, *)        = null
   * StringUtils.replace(&quot;&quot;, *, *)          = &quot;&quot;
   * StringUtils.replace(&quot;any&quot;, null, *)    = &quot;any&quot;
   * StringUtils.replace(&quot;any&quot;, *, null)    = &quot;any&quot;
   * StringUtils.replace(&quot;any&quot;, &quot;&quot;, *)      = &quot;any&quot;
   * StringUtils.replace(&quot;aba&quot;, &quot;a&quot;, null)  = &quot;aba&quot;
   * StringUtils.replace(&quot;aba&quot;, &quot;a&quot;, &quot;&quot;)    = &quot;b&quot;
   * StringUtils.replace(&quot;aba&quot;, &quot;a&quot;, &quot;z&quot;)   = &quot;zbz&quot;
   * </pre>
   * 
   * @see #replace(String text, String repl, String with, int max)
   * @param text text to search and replace in, may be null
   * @param repl the String to search for, may be null
   * @param with the String to replace with, may be null
   * @return the text with any replacements processed, <code>null</code> if
   *         null String input
   */
  public static String replace(String text, String repl, String with) {
    return replace(text, repl, with, -1);
  }

  /**
   * <p>
   * Replaces a String with another String inside a larger String, for the first
   * <code>max</code> values of the search String.
   * </p>
   * <p>
   * A <code>null</code> reference passed to this method is a no-op.
   * </p>
   * 
   * <pre>
   * StringUtils.replace(null, *, *, *)         = null
   * StringUtils.replace(&quot;&quot;, *, *, *)           = &quot;&quot;
   * StringUtils.replace(&quot;any&quot;, null, *, *)     = &quot;any&quot;
   * StringUtils.replace(&quot;any&quot;, *, null, *)     = &quot;any&quot;
   * StringUtils.replace(&quot;any&quot;, &quot;&quot;, *, *)       = &quot;any&quot;
   * StringUtils.replace(&quot;any&quot;, *, *, 0)        = &quot;any&quot;
   * StringUtils.replace(&quot;abaa&quot;, &quot;a&quot;, null, -1) = &quot;abaa&quot;
   * StringUtils.replace(&quot;abaa&quot;, &quot;a&quot;, &quot;&quot;, -1)   = &quot;b&quot;
   * StringUtils.replace(&quot;abaa&quot;, &quot;a&quot;, &quot;z&quot;, 0)   = &quot;abaa&quot;
   * StringUtils.replace(&quot;abaa&quot;, &quot;a&quot;, &quot;z&quot;, 1)   = &quot;zbaa&quot;
   * StringUtils.replace(&quot;abaa&quot;, &quot;a&quot;, &quot;z&quot;, 2)   = &quot;zbza&quot;
   * StringUtils.replace(&quot;abaa&quot;, &quot;a&quot;, &quot;z&quot;, -1)  = &quot;zbzz&quot;
   * </pre>
   * 
   * @param text text to search and replace in, may be null
   * @param repl the String to search for, may be null
   * @param with the String to replace with, may be null
   * @param max maximum number of values to replace, or <code>-1</code> if no
   *          maximum
   * @return the text with any replacements processed, <code>null</code> if
   *         null String input
   */
  public static String replace(String text, String repl, String with, int max) {
    if (text == null || isEmpty(repl) || with == null || max == 0) {
      return text;
    }

    StringBuffer buf = new StringBuffer(text.length());
    int start = 0, end = 0;
    while ((end = text.indexOf(repl, start)) != -1) {
      buf.append(text.substring(start, end)).append(with);
      start = end + repl.length();

      if (--max == 0) {
        break;
      }
    }
    buf.append(text.substring(start));
    return buf.toString();
  }

  // Empty checks
  // -----------------------------------------------------------------------
  /**
   * <p>
   * Checks if a String is empty ("") or null.
   * </p>
   * 
   * <pre>
   * StringUtils.isEmpty(null)      = true
   * StringUtils.isEmpty(&quot;&quot;)        = true
   * StringUtils.isEmpty(&quot; &quot;)       = false
   * StringUtils.isEmpty(&quot;bob&quot;)     = false
   * StringUtils.isEmpty(&quot;  bob  &quot;) = false
   * </pre>
   * 
   * <p>
   * NOTE: This method changed in Lang version 2.0. It no longer trims the
   * String. That functionality is available in isBlank().
   * </p>
   * 
   * @param str the String to check, may be null
   * @return <code>true</code> if the String is empty or null
   */
  public static boolean isEmpty(String str) {
    return str == null || str.length() == 0;
  }

  // Replace, character based
  // -----------------------------------------------------------------------
  /**
   * <p>
   * Replaces all occurrences of a character in a String with another. This is a
   * null-safe version of {@link String#replace(char, char)}.
   * </p>
   * <p>
   * A <code>null</code> string input returns <code>null</code>. An empty
   * ("") string input returns an empty string.
   * </p>
   * 
   * <pre>
   * StringUtils.replaceChars(null, *, *)        = null
   * StringUtils.replaceChars(&quot;&quot;, *, *)          = &quot;&quot;
   * StringUtils.replaceChars(&quot;abcba&quot;, 'b', 'y') = &quot;aycya&quot;
   * StringUtils.replaceChars(&quot;abcba&quot;, 'z', 'y') = &quot;abcba&quot;
   * </pre>
   * 
   * @param str String to replace characters in, may be null
   * @param searchChar the character to search for, may be null
   * @param replaceChar the character to replace, may be null
   * @return modified String, <code>null</code> if null string input
   * @since 2.0
   */
  public static String replaceChars(String str, char searchChar,
      char replaceChar) {
    if (str == null) {
      return null;
    }
    return str.replace(searchChar, replaceChar);
  }

  /**
   * <p>
   * Replaces multiple characters in a String in one go. This method can also be
   * used to delete characters.
   * </p>
   * <p>
   * For example:<br />
   * <code>replaceChars(&quot;hello&quot;, &quot;ho&quot;, &quot;jy&quot;) = jelly</code>.
   * </p>
   * <p>
   * A <code>null</code> string input returns <code>null</code>. An empty
   * ("") string input returns an empty string. A null or empty set of search
   * characters returns the input string.
   * </p>
   * <p>
   * The length of the search characters should normally equal the length of the
   * replace characters. If the search characters is longer, then the extra
   * search characters are deleted. If the search characters is shorter, then
   * the extra replace characters are ignored.
   * </p>
   * 
   * <pre>
   * StringUtils.replaceChars(null, *, *)           = null
   * StringUtils.replaceChars(&quot;&quot;, *, *)             = &quot;&quot;
   * StringUtils.replaceChars(&quot;abc&quot;, null, *)       = &quot;abc&quot;
   * StringUtils.replaceChars(&quot;abc&quot;, &quot;&quot;, *)         = &quot;abc&quot;
   * StringUtils.replaceChars(&quot;abc&quot;, &quot;b&quot;, null)     = &quot;ac&quot;
   * StringUtils.replaceChars(&quot;abc&quot;, &quot;b&quot;, &quot;&quot;)       = &quot;ac&quot;
   * StringUtils.replaceChars(&quot;abcba&quot;, &quot;bc&quot;, &quot;yz&quot;)  = &quot;ayzya&quot;
   * StringUtils.replaceChars(&quot;abcba&quot;, &quot;bc&quot;, &quot;y&quot;)   = &quot;ayya&quot;
   * StringUtils.replaceChars(&quot;abcba&quot;, &quot;bc&quot;, &quot;yzx&quot;) = &quot;ayzya&quot;
   * </pre>
   * 
   * @param str String to replace characters in, may be null
   * @param searchChars a set of characters to search for, may be null
   * @param replaceChars a set of characters to replace, may be null
   * @return modified String, <code>null</code> if null string input
   * @since 2.0
   */
  public static String replaceChars(String str, String searchChars,
      String replaceChars) {
    if (isEmpty(str) || isEmpty(searchChars)) {
      return str;
    }
    if (replaceChars == null) {
      replaceChars = "";
    }
    boolean modified = false;
    StringBuffer buf = new StringBuffer(str.length());
    for (int i = 0; i < str.length(); i++) {
      char ch = str.charAt(i);
      int index = searchChars.indexOf(ch);
      if (index >= 0) {
        modified = true;
        if (index < replaceChars.length()) {
          buf.append(replaceChars.charAt(index));
        }
      } else {
        buf.append(ch);
      }
    }
    if (modified) {
      return buf.toString();
    } else {
      return str;
    }
  }

}
